package com.zhoug.common3.fileselector.selector;

import com.zhoug.common3.content.FileType;
import com.zhoug.common3.fileselector.selector.impl.FileSelectorFactory;

import androidx.activity.ComponentActivity;
import androidx.fragment.app.Fragment;

/**
 * 构建文件选择器的工厂
 *
 * @Author 35574
 * @Date 2020/8/13
 * @Description
 */
public class FileSelectorBuilder {
    private ComponentActivity mActivity;
    private Fragment mFragment;
    private FileSelectedConfig mConfig;
    private int mFlag;
    private FileSelector.onSelectListener mOnSelectListener;

    public FileSelectorBuilder(ComponentActivity activity) {
        mActivity = activity;
        init();
    }

    public FileSelectorBuilder(Fragment fragment) {
        mFragment = fragment;
        init();
    }

    private void init() {
        mConfig = new FileSelectedConfig();
        mFlag = FileSelector.FLAG_ALL;
    }

    /**
     * 设置选择的文件类型 默认全部
     *
     * @param flag {@link FileSelector#FLAG_IMAGE ....}
     * @param flag
     */
    public FileSelectorBuilder setFlag(int flag) {
        this.mFlag = flag;
        return this;
    }

    /**
     * 设置是否可以调用摄像头拍摄
     *
     * @param camera
     * @return
     */
    public FileSelectorBuilder captureAll(boolean camera) {
        mConfig.captureImage = camera;
        mConfig.captureAudio = camera;
        mConfig.captureVideo = camera;
        return this;
    }

    /**
     * 设置是否可以调用摄像头拍摄
     *
     * @param camera
     * @return
     */
    public FileSelectorBuilder captureImage(boolean camera) {
        mConfig.captureImage = camera;
        return this;
    }

    /**
     * 设置是否可以调用摄像头拍摄
     *
     * @param camera
     * @return
     */
    public FileSelectorBuilder captureVideo(boolean camera) {
        mConfig.captureVideo = camera;
        return this;
    }

    /**
     * 设置是否可以调用摄像头拍摄
     *
     * @param camera
     * @return
     */
    public FileSelectorBuilder captureAudio(boolean camera) {
        mConfig.captureAudio = camera;
        return this;
    }

    /**
     * 设置最大选择的文件数
     *
     * @param maxNum
     * @return
     */
    public FileSelectorBuilder setMaxNum(int maxNum) {
        mConfig.maxNum = maxNum;
        return this;
    }

    /**
     * 设置选择的音视频最大长度
     *
     * @param maxDuration 毫秒
     * @return
     */
    public FileSelectorBuilder setMaxDuration(long maxDuration) {
        mConfig.maxDuration = maxDuration;
        return this;
    }

    /**
     * 设置选择的音视频最小长度
     *
     * @param minDuration 毫秒
     * @return
     */
    public FileSelectorBuilder setMinDuration(long minDuration) {
        mConfig.minDuration = minDuration;
        return this;
    }

    /**
     * 是否单张选择模式
     *
     * @param singleModel
     * @return
     */
    public FileSelectorBuilder singleModel(boolean singleModel) {
        mConfig.singleModel = singleModel;
        return this;
    }

    /**
     * 压缩图片
     *
     * @param compressImage
     * @return
     */
    public FileSelectorBuilder compressImage(boolean compressImage) {
        mConfig.compressImage = compressImage;
        return this;
    }

    /**
     * 压缩图片
     * @param compressWidth 压缩后的宽
     * @param compressHeight 压缩后的高
     * @param compressSize 压缩后的大小 小于此大小则不压缩
     * @return
     */
    public FileSelectorBuilder setCompressImageOption(int compressWidth,int compressHeight,int compressSize) {
        mConfig.compressWidth = compressWidth;
        mConfig.compressHeight = compressHeight;
        mConfig.compressSize = compressSize;
        return this;
    }

    /**
     * 设置监听器
     *
     * @param onSelectListener
     * @return
     */
    public FileSelectorBuilder setOnSelectListener(FileSelector.onSelectListener onSelectListener) {
        this.mOnSelectListener = onSelectListener;
        return this;
    }

    public IFileSelector build() {
        if (mFlag == FileSelector.FLAG_IMAGE
                || mFlag == FileSelector.FLAG_VIDEO
                || mFlag == FileSelector.FLAG_AUDIO
                || mFlag == FileSelector.FLAG_WORD
                || mFlag == FileSelector.FLAG_EXCEL
                || mFlag == FileSelector.FLAG_PDF
                || mFlag == FileSelector.FLAG_TXT
        ) {
            //类型文件选择
            FileSelector fileSelector;
            if (mFragment != null) {
                fileSelector = FileSelectorFactory.createFileChooser(mFragment);
            } else {
                fileSelector = FileSelectorFactory.createFileChooser(mActivity);
            }
            //配置
            FileSelector.Config config = new FileSelector.Config();
            if (mFlag == FileSelector.FLAG_IMAGE) {
                //图片
                config.fileType = FileType.IMAGE;
                config.isCamera = mConfig.captureImage;
                config.compressImage = mConfig.compressImage;
                config.compressWidth = mConfig.compressWidth;
                config.compressHeight = mConfig.compressHeight;
                config.compressSize = mConfig.compressSize;

            } else if (mFlag == FileSelector.FLAG_VIDEO) {
                //视频
                config.fileType = FileType.VIDEO;
                config.isCamera = mConfig.captureVideo;
                config.maxDuration = mConfig.maxDuration;
                config.minDuration = mConfig.minDuration;
            } else if (mFlag == FileSelector.FLAG_AUDIO) {
                //音频
                config.fileType = FileType.AUDIO;
                config.isCamera = mConfig.captureAudio;
                config.maxDuration = mConfig.maxDuration;
                config.minDuration = mConfig.minDuration;
            } else if (mFlag == FileSelector.FLAG_WORD) {
                //word
                config.fileType = FileType.WORD;
                config.isCamera = false;
            } else if (mFlag == FileSelector.FLAG_EXCEL) {
                //excel
                config.fileType = FileType.EXCEL;
                config.isCamera = false;
            } else if (mFlag == FileSelector.FLAG_PDF) {
                //pdf
                config.fileType = FileType.PDF;
                config.isCamera = false;
            } else if (mFlag == FileSelector.FLAG_TXT) {
                //txt
                config.fileType = FileType.TXT;
                config.isCamera = false;
            }
            config.maxNum = mConfig.maxNum;
            config.singleModel = mConfig.singleModel;
            fileSelector.setConfig(config);
            fileSelector.setOnSelectListener(mOnSelectListener);
            return fileSelector;
        } else {
            //多个类型文件选择
            FileSelectorDialog fileSelectorDialog;
            if (mFragment != null) {
                fileSelectorDialog = new FileSelectorDialog(mFragment);
            } else {
                fileSelectorDialog = new FileSelectorDialog(mActivity);
            }
            fileSelectorDialog.setFlag(mFlag);
            fileSelectorDialog.setFileSelectedConfig(mConfig);
            fileSelectorDialog.setOnSelectListener(mOnSelectListener);
            return fileSelectorDialog;
        }
    }


}
